package cn.controller;

import cn.service.impl.AttributeServiceImpl;
import entity.Category;
import entity.DTO;
import entity.Result;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@CrossOrigin
public class AttributeController {
    @Resource
    AttributeServiceImpl attributeService;

    @RequestMapping("/addAttribute")
    public Result addAttribute(@RequestBody Object object) {
        String string = object.toString();//一级分类level0+二级分类level1
        //获取字符串中前端输入的名称
        String fullName = string.substring("{fullName=".length(), string.length() - 1);
        //必须按照xx-xx格式输入,否则打回
        String regex = ".+-.+";
        boolean matches = fullName.matches(regex);
        if (!matches) {
            return Result.fail(500,"输入格式错误,参考格式为'xx-xx'");
        } else {
            //分别获取-左边的字符串和-右边的字符串
            String[] split = fullName.split("-", 2);
            String leftStr = split[0];//前端输入-符号左边的字符串(category表level0)
            String rightStr = split[1];//前端输入-符号右边的字符串(category表level1)
            //查询leftStr,rightStr是否都存在
            boolean b1 = attributeService.queryByLeftStrAndLevel(leftStr, 0);
            boolean b2 = attributeService.queryByRightStrAndLevel(rightStr, 1);
            if (b1 && b2) {
                return Result.fail(500,"类型名称已存在!");
            } else if (b1) {
                //存在level0级字段,先根据leftStr获取level0级字段主键id
                //level1级字段不存在,允许添加level1级字段,设置level1,parent_id为level0级主键
                Long leftStrId = attributeService.queryIdByLeftStr(leftStr, 0);
                Category category = new Category(leftStrId,rightStr,1);
                //添加字段
                Result result = attributeService.insertLeftStr(category);
                return result;
            }
            return null;
        }
    }

    @RequestMapping("/queryAllAttribute")
    public Result queryAllAttribute(Integer currentPage,Integer pageSize) {
        Integer level = 1;
        return attributeService.queryByLevel(level,currentPage,pageSize);
    }

    @RequestMapping("/attributeEdit")
    public Result attributeEdit(@RequestBody DTO dto) {
        Long categoryId = dto.getCategory_id();//level1的category_id
        //获取字符串中前端输入的名称
        String fullName = dto.getFullName();
        //必须按照xx-xx格式输入,否则打回
        String regex = ".+-.+";
        boolean matches = fullName.matches(regex);
        if (!matches) {
            return Result.fail(500,"输入格式错误,参考格式为'xx-xx'");
        } else {
            //分别获取-左边的字符串和-右边的字符串
            String[] split = fullName.split("-", 2);
            String leftStr = split[0];//前端输入-符号左边的字符串(category表level0)
            String rightStr = split[1];//前端输入-符号右边的字符串(category表level1)
            //根据level1的parent_id获取Left的主键id
            Category category = attributeService.selectByPrimaryKey(categoryId);
            Long leftId = category.getParent_id();
            Category categoryLeft = new Category(leftId,leftStr);
            Category categoryRight = new Category(categoryId,rightStr);
            Result resultRight = attributeService.updateByPrimaryKeySelective(categoryRight);
            Result resultLeft = attributeService.updateByPrimaryKeySelective(categoryLeft);
            if (resultRight.getCode() == 200 && resultLeft.getCode() == 200) {
                return Result.success(200,"编辑成功!");
            } else {
                return Result.fail(500,"编辑失败");
            }
        }
    }

    @RequestMapping("/deleteAttributeRow")
    public Result deleteRow(Long category_id) {
        return attributeService.deleteRow(category_id);
    }

    @RequestMapping("/deleteAttributeListRow")
    public Result deleteAttributeListRow(Integer attribute_id) {
        return attributeService.deleteAttributeListRow(attribute_id);
    }

    @RequestMapping("/attributeListEdit")
    public Result attributeListEdit(@RequestBody DTO dto) {
        return attributeService.attributeListEdit(dto);
    }
}
